﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using L2C;
using L2C.Models.Administrator;

namespace L2C.Controllers.Administrator
{ 
    [Authorize]
    public class PublishItemController : Controller
    {
        private L2CEntities db = new L2CEntities();

        //
        // GET: /PubishItem/

        public ViewResult Index()
        {
            var publishing_item = db.Publishing_Item.Include("Product").Include("Category_Item");
            return View(publishing_item.OrderByDescending(x => x.StartDate).ToList());
        }

        //
        // GET: /PubishItem/Details/5

        public ViewResult Details(int id)
        {
            Publishing_Item publishing_item = db.Publishing_Item.Single(p => p.Item_Id == id);
            return View(publishing_item);
        }

        //
        // GET: /PubishItem/Create

        public ActionResult Create()
        {
            ViewBag.ProductId = new SelectList(db.Products, "Product_Id", "Product_Name");
            ViewBag.CategoryId = new SelectList(db.Category_Item, "Category_Id", "Category_Name");
            return View();
        } 

        //
        // POST: /PubishItem/Create

        [HttpPost]
        public ActionResult Create(PublishItemModel publishing_item)
        {
            if (ModelState.IsValid)
            {
                Publishing_Item item = publishing_item.PopulateToEntity();                
                db.Publishing_Item.AddObject(item);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ProductId = new SelectList(db.Products, "Product_Id", "Product_Name", publishing_item.ProductId);
            ViewBag.CategoryId = new SelectList(db.Category_Item, "Category_Id", "Category_Name", publishing_item.CategoryId);
            return View(publishing_item);
        }
        
        //
        // GET: /PubishItem/Edit/5
 
        public ActionResult Edit(int id)
        {
            PublishItemModel publishing_item = PublishItemModel.Parse(db.Publishing_Item.SingleOrDefault(p => p.Item_Id == id));
            ViewBag.ProductId = new SelectList(db.Products, "Product_Id", "Product_Name", publishing_item.ProductId);
            ViewBag.CategoryId = new SelectList(db.Category_Item, "Category_Id", "Category_Name", publishing_item.CategoryId);
            return View(publishing_item);
        }

        //
        // POST: /PubishItem/Edit/5

        [HttpPost]
        public ActionResult Edit(PublishItemModel publishing_item)
        {
            if (ModelState.IsValid)
            {
                Publishing_Item item = publishing_item.PopulateToEntity();
                db.Publishing_Item.Attach(item);
                db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProductId = new SelectList(db.Products, "Product_Id", "Product_Name", publishing_item.ProductId);
            ViewBag.CategoryId = new SelectList(db.Category_Item, "Category_Id", "Category_Name", publishing_item.CategoryId);
            return View(publishing_item);
        }

        //
        // GET: /PubishItem/Delete/5
 
        public ActionResult Delete(int id)
        {
            Publishing_Item publishing_item = db.Publishing_Item.Single(p => p.Item_Id == id);
            return View(publishing_item);
        }

        //
        // POST: /PubishItem/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Publishing_Item publishing_item = db.Publishing_Item.Single(p => p.Item_Id == id);
            db.Publishing_Item.DeleteObject(publishing_item);
            db.SaveChanges();
            return RedirectToAction("Index");
        }        

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}